﻿using GeoTimeZone;
using HDS.Common.Helper;
using HDS.Data;
using HDS.Model;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using TimeZoneConverter;

namespace CreateEntity
{
    class Program
    {
        static void Main(string[] args)
        {
            new AppSettings();

            //string tz = TimeZoneLookup.GetTimeZone(121.456924, 31.037943).Result;
            //string wz = TZConvert.IanaToWindows(tz);

            var Db = new MasterDbContext().Db;

            foreach (var item in Db.DbMaintenance.GetTableInfoList())
            {
                string className = GetPascalCaseName(item.Name);
                //Db.MappingTables.Add(entityName, item.Name);
                //Console.WriteLine(entityName);

                var classSb = new StringBuilder();
                classSb.AppendLine("using SqlSugar;");
                classSb.AppendLine("using System;");
                classSb.AppendLine("using System.Collections.Generic;");
                classSb.AppendLine("using System.Text;");
                classSb.AppendLine("");
                classSb.AppendLine("namespace HDS.Model.Entity.Master");
                classSb.AppendLine("{");
                if (!string.IsNullOrEmpty(item.Description.Trim()))
                {
                    classSb.AppendLine("    /// <summary>");
                    classSb.AppendLine($"    /// {item.Description}");
                    classSb.AppendLine("    /// </summary>");
                }
                classSb.AppendLine($"    [SugarTable(\"{item.Name}\")]");
                classSb.AppendLine($"    public class {className}");
                classSb.AppendLine("    {");
                foreach (var col in Db.DbMaintenance.GetColumnInfosByTableName(item.Name))
                {
                    classSb.AppendLine("");
                    var propName = GetPascalCaseName(col.DbColumnName);
                    if (!string.IsNullOrEmpty(col.ColumnDescription.Trim()))
                    {
                        classSb.AppendLine("        /// <summary>");
                        classSb.AppendLine($"        /// {col.ColumnDescription}");
                        classSb.AppendLine("        /// </summary>");
                    }

                    if (col.IsPrimarykey)
                    {
                        classSb.AppendLine($"        [SugarColumn(IsPrimaryKey = true, IsIdentity = true, ColumnName = \"{col.DbColumnName}\")]");
                    }
                    else
                    {
                        classSb.AppendLine($"        [SugarColumn(ColumnName = \"{col.DbColumnName}\")]");
                    }

                    classSb.Append($"        public {GetType(col.DataType)} {propName} {{ get; set; }}");
                    if (!string.IsNullOrEmpty(col.DefaultValue))
                    {
                        var needQuote = NeedQuote(col.DataType);
                        classSb.Append($" = {(needQuote ? "\"" : "")}{col.DefaultValue}{(needQuote ? "\"" : "")};");
                    }
                    classSb.AppendLine("");
                    //Db.MappingColumns.Add(col.DbColumnName.ToUpper(), col.DbColumnName, entityName);
                    //Console.WriteLine("----" + GetPascalCaseName(col.DbColumnName));
                }
                classSb.AppendLine("");
                classSb.AppendLine("    }");

                classSb.AppendLine("}");

                Console.WriteLine(classSb);
                //Console.ReadLine();

                File.WriteAllText($"c:/demo/10/{className}.cs", classSb.ToString());
            }


            foreach (var item in Db.DbMaintenance.GetTableInfoList())
            {
                string className = GetPascalCaseName(item.Name);

                var editHtmlSb = new StringBuilder();
                foreach (var col in Db.DbMaintenance.GetColumnInfosByTableName(item.Name))
                {
                    if (col.DbColumnName.IndexOf("updated_") == 0)
                        continue;

                    var propName = GetPascalCaseName(col.DbColumnName);
                    editHtmlSb.AppendLine($"<tr><th>{col.ColumnDescription?.Trim()}</th>");
                    editHtmlSb.AppendLine($"<td><input class=\"easyui-textbox\" name=\"{propName}\" data-options=\"required: true\" style=\"width: 50%;\" /></td></tr>");
                }

                Console.WriteLine(editHtmlSb);

                File.WriteAllText($"c:/demo/10/{className}.txt", editHtmlSb.ToString());
            }

            Console.ReadLine();
        }



        static string GetPascalCaseName(string name)
        {
            if (!name.Contains(" "))
            {
                name = Regex.Replace(name, "(?<=[a-z])(?=[A-Z])", " ");
            }
            string s = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(name.ToLower()).Replace(" ", "").Replace("_", "");

            return s;
        }

        static string GetType(string dataType)
        {
            switch (dataType)
            {
                case "varchar":
                case "text":
                    return "string";
                case "int":
                    return "int";
                case "date":
                case "datetime":
                    return "DateTime";
                case "decimal":
                    return "decimal";
                case "float":
                    return "float";
                case "tinyint":
                    return "int";
                default:
                    return "";
            }
        }

        static bool NeedQuote(string dataType)
        {
            switch (dataType)
            {
                case "varchar":
                case "text":
                case "date":
                case "datetime":
                    return true;
                case "int":
                case "decimal":
                case "float":
                case "tinyint":
                    return false;
                default:
                    return true;
            }
        }
    }
}